package com.example.allshengmedical.welcome.presenter;

import android.R.anim;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.ImageView;
import android.widget.Toast;

import com.allsheng.widget.AllShengDialog;
import com.allsheng.widget.AllShengDialog.ClickListenerInterface;
import com.example.allshengmedical.base.BaseApplication;
import com.example.allshengmedical.mainmenu.activity.MainMenuActivity;
import com.example.allshengmedical.util.ByAlert;
import com.example.allshengmedical.util.LogUtil;
import com.example.allshengmedical.util.SharedPreferencesUtils;
import com.example.allshengmedical.welcome.activity.WelcomeActivity;
import com.example.allshengmedical.welcome.biz.IWelcomBiz;
import com.example.allshengmedical.welcome.biz.OnLoginListener;
import com.example.allshengmedical.welcome.biz.WelcomBiz;
import com.example.allshengmedical.welcome.view.IWelcomeView;
import com.itboye.allshengmedical.sci.DefaultSerialControl;
import com.itboye.allshengmedical.sci.ISerialControl;
import com.itboye.allshengmedical.sci.cmd.req.CmdType;
import com.itboye.allshengmedical.sci.packet.resp.BaseRespPacket;
import com.itboye.allshengmedical.sci.packet.resp.SelfCheckRespPacket;

public class WelcomPresenter {
	private IWelcomeView welcomeView;
	private IWelcomBiz welcomBiz;
	private final int OUTOFTIME = 300012;
	ImageView bigView, smallView;
	Activity activity;
	int count;
	// 端口
//	private ISerialControl controlInstance;

	public IWelcomeView getWelcomeView() {
		return welcomeView;
	}

//	private Handler mHandler = new Handler() {
//		@Override
//		public void handleMessage(Message msg) {
//			super.handleMessage(msg);
//			switch (msg.what) {
//			case ISerialControl.FindPacketMessage:
//				Log.e("hu", "收到了返回数据");
//				// 完成主界面更新,拿到数据
//				update(msg.getData());
//				break;
//			case ISerialControl.SCTimeoutMessage:
//				// ByAlert.alert("自检请求超时,请重试");
//				AllShengDialog allShengDialog = new AllShengDialog(activity,
//						"", "自检请求超时", "OK");
//				allShengDialog.show();
//				controlInstance.pause();
//				allShengDialog.setClicklistener(new ClickListenerInterface() {
//
//					@Override
//					public void OK() {
//						// TODO Auto-generated method stub
//						Intent intent2 = new Intent(activity,
//								MainMenuActivity.class);
//						activity.startActivity(intent2);
//						activity.finish();
//					}
//				});
//
//			default:
//				break;
//			}
//
//		};
//	};

	public WelcomPresenter(IWelcomeView welcomeView, ImageView bigView,
			ImageView smallView, Activity activity) {
		super();
		this.welcomeView = welcomeView;
		this.bigView = bigView;
		this.smallView = smallView;
		this.activity = activity;
		welcomBiz = new WelcomBiz();
		SharedPreferencesUtils.remove(activity, BaseApplication.SELF_CHECK);
//		controlInstance = DefaultSerialControl.getInstance();
//		controlInstance.init();
//		controlInstance.start();
//		controlInstance.addCommand(CmdType.SelfCheck, null, 0);
		login();
	}

//	protected void update(Bundle bundle) {
//		// TODO Auto-generated method stub
//		String cmdType = bundle.getString("cmdType");
//
//		BaseRespPacket packet = (BaseRespPacket) bundle
//				.getSerializable("packet");
//		if (cmdType.length() < 1)
//			return;
//		// 根据type 转换packet 为你想要的
//		CmdType type = CmdType.getCmdType(cmdType.substring(1));
//
//		// 发送指令后这里会收到数据
//		Log.d("read_data", "接受到的响应数据包是" + cmdType + type.toDesc());
//
//		// ByAlert.alert("接受到的响应数据包类型" + cmdType + "," + type.toDesc());
//		switch (type) {
//
//		case SelfCheck:
//			// 转换成自检数据包
//			SelfCheckRespPacket selfCheckRespPacket = new SelfCheckRespPacket(
//					packet);
//			if (packet != null) {
//				Log.e("hu...return_data", selfCheckRespPacket.getErrorDesc());
//				if (selfCheckRespPacket.isSuccess()) {
//					Intent intent = new Intent(activity, MainMenuActivity.class);
//					activity.startActivity(intent);
//					activity.finish();
//					controlInstance.pause();
//				} else {
//					AllShengDialog allShengDialog = new AllShengDialog(
//							activity, "自检请求错误信息",
//							selfCheckRespPacket.getErrorDesc(), "OK");
//					allShengDialog.show();
//					controlInstance.pause();
//					allShengDialog
//							.setClicklistener(new ClickListenerInterface() {
//
//								@Override
//								public void OK() {
//									// TODO Auto-generated method stub
//									Intent intent = new Intent(activity,
//											MainMenuActivity.class);
//									activity.startActivity(intent);
//									activity.finish();
//								}
//							});
//
//				}
//				// Toast.makeText(BaseApplication.getContext(),
//				// "【自检错误信息】" + selfCheckRespPacket.getErrorDesc(),
//				// Toast.LENGTH_LONG).show();
//				// Log.e("hu...self", selfCheckRespPacket.getErrorDesc());
//				// Log.e("hu...self2", selfCheckRespPacket.ModeOneTemp());
//			} else {
//				// mHandler.sendEmptyMessageDelayed(OUTOFTIME, 5000);
//			}
//			break;
//		}
//	}

	public void login() {
//		controlInstance.setHandler(mHandler);
		welcomBiz.login(new OnLoginListener() {

			@Override
			public void loginSuccess() {

			}

			@Override
			public void loginFailed() {
				// TODO Auto-generated method stub

			}
		});

	}

	public void spin() {
		welcomeView.spin(bigView, smallView);
	}

}
